import httpUtil from '@/utils/request.js'
import address from '@/utils/address.js'
import store from '@/store/store.js'

export const getSysAvatar = () => {
   return httpUtil({
      url: '/file/avatar',
      method: 'get'
   })
}

export const uploadFile = ({url = '/file/upload/image', fileType = 'image', data}) => {
   return new Promise((resolve, reject) => {
      uni.uploadFile({
         url: address.baseURL + url,
         header: {
            OS: 'app',
            token: store.getters.token
         },
         fileType: fileType,
         files: [],
         formData: {
            ...data
         },
         success: (res) => {
            if(res.statusCode === 200 && res.data) {
               resolve(JSON.parse(res.data))
            }else {
               uni.showToast({
                  title: '上传失败',
                  icon: 'none',
                  mask: true
               })
            }
         },
         fail: (error) => {
            uni.showToast({
               title: '上传失败',
               icon: 'none',
               mask: true
            })
         }
      })
   })
}